package com.example.SearchZB;

import android.app.ProgressDialog;
import android.content.Context;
import android.util.Log;
import android.widget.TextView;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.model.LatLng;


/**
 * Created by zdz on 2014/12/2.
 */
public class LocationApplication{
    public static LatLng currentLat;
    private LocationClient locationClient = null;
    private BDLocationListener bdLocationListener = null;

    //    public static LatLng cureentLat;
    String cureentAdress;
    //用来接着主界面传来的参数
    private Context context;
    private TextView textView;

    private ProgressDialog progressDialog;


    public LocationApplication(Context context, TextView textView) {
        this.context = context;
        this.textView = textView;
    }
    public void getCurrentLocation() {
        textView.setText("定位中,请稍后...");
        progressDialog = new ProgressDialog(context);
        progressDialog.setMessage("定位中...");
        progressDialog.show();

        locationClient = new LocationClient(context);
        setLocationOption();
        bdLocationListener = new MyLocationListener();
        locationClient.registerLocationListener(bdLocationListener);

        //启动定位SDK
        locationClient.start();
        //发起定位
        locationClient.requestLocation();

    }

    private void setLocationOption() {
        LocationClientOption option = new LocationClientOption();

        option.setOpenGps(true);//打开GPS
        option.setScanSpan(0);//设置定时定位的时间间隔,毫秒
        option.setIsNeedAddress(true);//返回的定位结果包含地址信息
        //option.setNeedDeviceDirect(true);//返回的定位结果包含地址信息
        option.setCoorType("bd09ll");//(设置返回值的坐标类型)返回的定位结果是百度经纬度,默认值gcj02

        locationClient.setLocOption(option);
    }


    private class MyLocationListener implements BDLocationListener {
        @Override
        public void onReceiveLocation(BDLocation bdLocation) {
            if (bdLocation == null) {
                return;
            }

            currentLat=new LatLng(bdLocation.getLatitude(),bdLocation.getLongitude());
            cureentAdress = bdLocation.getAddrStr();
            textView.setText(cureentAdress);
            progressDialog.dismiss();
        }
    }
    public void stopLocation() {
        if (locationClient != null|| locationClient.isStarted()) {
            locationClient.stop();
        }
    }
}